var ref = require('ref'); //数据指针
var ffi = require('ffi'); //使用Javascript调用动态库
var refStruct = require('ref-struct'); //结构体
var refArray = require('ref-array'); // 用Buffer来实现C语言中的 array 数据类型

var ID_CODE = refArray('char', 6);

var ID_CODE = refArray('char', 6);

var ID_PASS = refArray('char', 6);

/**
 * 执法记录仪信息
 */
var ZFY_INFO = refStruct({
    'cSerial': refArray('char', 8),
    'userNo': refArray('char', 7),
    'userName': refArray('char', 33),
    'unitNo': refArray('char', 13),
    'unitName': refArray('char', 33)
});
/**
 * 执法记录仪密码
 */
var ZFY_PASSWD = refStruct({
    'userType': ref.types.uchar,
    'Manage_passwd': refArray('char', 6),
    'User_passwd': refArray('char', 6),
    'Default_passwd': refArray('char', 6)
});
/**
 * 执法记录仪参数
 */
var ZFY_PARAM = refStruct({
    'dev_param': refArray('char', 512)
});
/**
 * 对动态库里的函数进行声明
 */
var zfyInfoLib = new ffi.Library('./ZFYINFO', {
    'GetIDCode': ['int32', [ref.refType(ID_CODE), ref.refType('ushort')]],
    'Init_Device': ['int32', [ref.refType(ID_CODE), ref.refType('ushort')]],
    'GetZFYPassword': ['int32', [ref.refType(ZFY_PASSWD), ref.refType('ushort')]],
    'GetZFYInfo': ['int32', [ref.refType(ZFY_INFO), 'string', ref.refType('ushort')]],
    'WriteZFYInfo': ['int32', [ref.refType(ZFY_INFO), 'string', ref.refType('ushort')]],
    'ReadDeviceBatteryDumpEnergy':['int32', [ ref.refType('int32'), 'string', ref.refType('ushort')]],
    "GetDevParam_Resolution": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_PhotoSize": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Recording":['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Shutdown": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Infrared": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Video_quality": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Video_split_time": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Post_recording": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Lcd_brightness": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "GetDevParam_Capture": ['int32', [ref.refType(ZFY_PARAM), 'string', ref.refType('ushort')]],
    "SetMSDC":['int32', ['string', ref.refType('ushort')]]
    //"Get_Version_Info": ['int32', [ref.refType(ZFY_PARAM), ref.refType('ushort')]]
});


function ZFJLY() {
    
}
/**
 * 设备实例化
 */
ZFJLY.prototype.Init_Device = function () {
    //实例化
    var idCode = ref.alloc(ID_CODE);
    var outNumber = ref.alloc('ushort');
    var bool = zfyInfoLib.Init_Device(idCode, outNumber);
    var result = outNumber.deref();
}


/**
 * 设备ID
 */
ZFJLY.prototype.GetIDCode = function () {
    var idCode = ref.alloc(ID_CODE);
    var outNumber = ref.alloc('ushort');
    var bool = zfyInfoLib.GetIDCode(idCode, outNumber);
    var result = outNumber.deref();
}
/**
 * 密码
 */
ZFJLY.prototype.GetZFYPassword  = function (callback){
    var zfyPasswd1 = ref.alloc(ZFY_PASSWD);
    var outNumber = ref.alloc('ushort');
    zfyPasswd1.ref().userType = 1;
    var bool = zfyInfoLib.GetZFYPassword(zfyPasswd1, outNumber);
    var result = outNumber.deref();
    this.zfyPasswd = zfyPasswd1.deref();
    var _zfyPassword = {
        Default_passwd: asciiTransforBinary(this.zfyPasswd.Default_passwd),
        Manage_passwd: asciiTransforBinary(this.zfyPasswd.Default_passwd),
        User_passwd: asciiTransforBinary(this.zfyPasswd.Default_passwd),
        userType: this.zfyPasswd.userType
    }
    if(!callback) return
    callback(_zfyPassword)
}
/**
 * 连接初始化
 */
ZFJLY.prototype.ConnectZFJLY = function () {
    this.Init_Device()
    this.GetIDCode()
    this.GetZFYPassword()
    this.SetMSDC()
}
/**
 * 确认硬盘
 */
ZFJLY.prototype.SetMSDC = function () {
    var outNumber = ref.alloc('ushort');
    var bool = zfyInfoLib.SetMSDC(asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber);
    var result = outNumber.deref();
}
/**
 * 获取执法记录仪信息
 */
// ZFJLY.prototype.GetZFYInfo = function (callback) {
//     var zfyInfo = ref.alloc(ZFY_INFO);
//     var outNumber = ref.alloc('ushort');
//     var bool = zfyInfoLib.GetZFYInfo(zfyInfo, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber);
//     var result = outNumber.deref();
//     var _zfyInfo = zfyInfo.deref();
//     var userZfyInfo ={
//         cSerial: asciiTransforBinary(_zfyInfo.cSerial),
//         unitName: asciiTransforUcs2(_zfyInfo.unitName),
//         unitNo: asciiTransforBinary(_zfyInfo.unitNo),
//         userName: asciiTransforUcs2(_zfyInfo.userName),
//         userNo: asciiTransforBinary(_zfyInfo.userNo)
//     }
//     callback(userZfyInfo)
// } 
ZFJLY.prototype.GetZFYInfo = function (callback) {
    var zfyInfo = ref.alloc(ZFY_INFO);
    var outNumber = ref.alloc('ushort');
    var bool = zfyInfoLib.GetZFYInfo(zfyInfo, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber);
    var result = outNumber.deref();
    var _zfyInfo = zfyInfo.deref();
    var userZfyInfo ={
        cSerial: asciiTransforUcs2(_zfyInfo.cSerial),
        unitName: asciiTransforUcs2(_zfyInfo.unitName),
        unitNo: asciiTransforUcs2(_zfyInfo.unitNo),
        userName: asciiTransforUcs2(_zfyInfo.userName),
        userNo: asciiTransforUcs2(_zfyInfo.userNo)
    }
    callback(userZfyInfo)
} 
/**
 * 设置执法记录仪信息
 */

ZFJLY.prototype.WriteZFYInfo = function () {
    zfyInfo = ref.alloc(ZFY_INFO);
   
    zfyInfo.cSerial = '0001';
    zfyInfo.userNo = 'z0007';
    zfyInfo.userName ='张三';
    zfyInfo.unitNo = '330012000';
    zfyInfo.unitName = '一大队一中队';

    var outNumber = ref.alloc('ushort');

    var bool = zfyInfoLib.WriteZFYInfo(zfyInfo, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber);
    var result = outNumber.deref();
    zfyInfo = zfyInfo.deref();
   
  
}
ZFJLY.prototype.ReadDeviceBatteryDumpEnergy = function (callback) {
    var zfjlyBattery = ref.alloc('int32')
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.ReadDeviceBatteryDumpEnergy(zfjlyBattery, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyBatteryInfor = {
        BatteryElect: zfjlyBattery.deref()
    }
    callback(zfjlyBatteryInfor)
} 

ZFJLY.prototype.GetDevParam_Resolution = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Resolution (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforBinary(zfjlyParam.dev_param)
    }
    console.log(data)
    callback(data) 
}

ZFJLY.prototype.GetDevParam_PhotoSize = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_PhotoSize (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
 
    var data = {
        dev_param: asciiTransforBinary(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Recording = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Recording (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Shutdown = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Shutdown (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Infrared = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Infrared (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Video_quality = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Video_quality (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Video_split_time = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Video_split_time (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Post_recording = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Post_recording (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Lcd_brightness = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Lcd_brightness (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

ZFJLY.prototype.GetDevParam_Capture = function (callback) {
    var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
    var outNumber = ref.alloc('ushort')
    var bool = zfyInfoLib.GetDevParam_Capture (zfjly_PARAM, asciiTransforBinary(this.zfyPasswd.Manage_passwd), outNumber)
    var zfjlyParam = zfjly_PARAM.deref()
    var data = {
        dev_param: asciiTransforUcs2(zfjlyParam.dev_param)
    }
    callback(data) 
}

// ZFJLY.prototype.Get_Version_Info = function (callback) {
//     var zfjly_PARAM  = ref.alloc(ZFY_PARAM)
//     var outNumber = ref.alloc('ushort')
//     var bool = zfyInfoLib.Get_Version_Info (zfjly_PARAM, TEXT(this.zfyPasswd.Manage_passwd), outNumber)
//     var zfjlyParam = zfjly_PARAM.deref()
//     console.log(zfjlyParam)
//     var data = {
//         dev_param: UCS2TEXT(zfjlyParam.dev_param)
//     }
//     callback(data) 
// }



